<template>
  <div id="container-msg">
<!--    <CodeHeader :title="$route.meta.name"></CodeHeader>-->
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <div v-if="showMsg" class="msg-no-data">
        <img src="../../assets/img/no_data.png" width="65">
        <p>暂无消息</p>
      </div>
      <van-list
        v-model="loading"
        :finished="finished"
        v-else
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div v-show="!showMsg" class="notice-card" v-for="(item, index) in msgList" :key="index">
          <div
            style="display: flex;align-items: center;justify-content: space-between;padding: 10px 15px;"
          >
            <div style="display: flex;align-items: center;width: 50%;">
              <img src="../../assets/img/bssz.png" width="20" v-if="item.title && (item.title).indexOf('山庄') !== -1">
              <img src="../../assets/img/bus.png" width="20" v-if="item.title && (item.title).indexOf('公交') !== -1">
              <img src="../../assets/img/yibao.png" width="20" v-if="item.title && (item.title).indexOf('医保') !== -1">
              <p style="font-size: 16px;font-weight: 600;padding-left: 8px;width: 120px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
                {{ item.title }}
              </p>
            </div>
            <p style="font-size: 13px;color: #666;width: 50%;text-align: right;">
              {{ item.time }}
            </p>
          </div>
          <div style="border-bottom: 1px solid #eee;"></div>
          <div style="text-align: center;padding-top: 20px;">
            <p v-if="item.desc && !item.amount" style="color: #666666;padding-bottom: 30px;">
              {{ item.desc }}
            </p>
            <p v-if="item.desc && item.amount" style="color: #666666;">
              {{ item.desc }}
            </p>
            <div
              v-if="item.amount"
              style="display: flex;align-items: baseline;justify-content: center;padding: 5px 0 16px 0;"
            >
              <!--              <p style="color: #4663ff;font-size: 14px;">￥</p>-->
              <p style="color: #4663ff;font-size: 24px;font-weight: 600;">￥{{ item.amount }}</p>
            </div>
            <div style="border-bottom: 1px solid #eee;"></div>
            <div
              style="display: flex;align-items: center;justify-content: space-between;padding: 10px 15px;"
              @click="goDetail(item)"
            >
              <p style="color: #4663ff;font-size: 14px;padding-top: 3px;">查看详情</p>
              <div>
                <img src="../../assets/img/blue_right.png" width="7" height="12" />
              </div>
            </div>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import nus from "@nsrd/nus";
import CodeHeader from "@/components/code/Header";
export default {
  name: "CodeMineNotice",
  components: {
    CodeHeader
  },
  data() {
    return {
      selectDate: nus.date.format(new Date(), "YYYYMM"),
      searchInfo: {
        yearMonth: "",
        pageNo: 0,
        pageSize: 4
      },
      totalPages: "",
      msgList: [],
      showMsg: false,
      loading: false,
      finished: false,
      refreshing: false
    };
  },
  created() {
    this.initData();
  },
  mounted() {
    document.title = "我的消息";
  },
  methods: {
    initData() {
      this.getMsg();
    },
    onRefresh() {
      this.finished = false;
      this.loading = true;
      this.searchInfo.pageNo = 0;
      this.msgList = [];
      this.getMsg();
    },
    onLoad() {
      if (this.searchInfo.pageNo <= this.totalPages - 1) {
        this.searchInfo.pageNo += 1;
        this.getMsg();
      } else {
        this.finished = true;
      }
    },
    async getMsg() {
      let res = await this.api.obtainMsg(this.searchInfo);
      let arry = [];
      if (res.data.length > 0) {
        arry = res.data;
      }
      if (this.msgList.length !== 0) {
        for (let i = 0; i < arry.length; i++) {
          this.msgList.push(arry[i]);
        }
      } else {
        this.msgList = res.data;
      }
      this.msgList.forEach(item => {
        item.msgData1 = JSON.parse(item.msgData);
      });
      this.msgList = await this.transItem(this.msgList);
      this.showMsg = this.msgList.length <= 0;
      this.totalPages = res.totalPages;
      this.loading = false;
      this.finished = false;
      this.refreshing = false;
    },
    async transItem(data) {
      let arr = [];
      for (let j = 0; j < data.length; j++) {
        let res = await this.api.msgTemplate(data[j].templateId);
        if (res) {
          if (res.data) {
            let p = JSON.parse(res.data.content);
            for (let key in p) {
              let str = p[key];
              data[j][key] = data[j].msgData1[str];
            }
            arr.push(data[j]);
          }
        }
      }
      return arr;
    },
    goDetail(data) {
      sessionStorage.setItem("notice_detail", JSON.stringify(data));
      this.$router.push("/code/mine/notice/detail");
    }
  }
};
</script>

<style scoped lang="less">
#container-msg {
  width: 100%;
  height: 100%;
  position: fixed;
  overflow: auto;
  background: #F6F7F9;

  .msg-no-data {
    text-align: center;
    margin-top: 55px;
    p {
      font-size: 15px;
      letter-spacing: 1px;
      color: #333;
    }
  }

  .notice-card {
    background: #fff;
    margin: 15px 15px;
    padding: 20px 0 10px 0;
    border-radius: 4px;
  }
}
p {
  margin: 0;
  padding: 0;
}
</style>
